import { NCard } from "naive-ui";
import type { CardProps } from "naive-ui";
import type { BaseComponentType } from "../types";
import type { HTMLAttributes } from "vue";
import { getSlotsDom } from "../utils";
export type CardFactoryConstructorType = {
  props?: CardProps;
  defaultSlot?: BaseComponentType;
  coverSlot?: BaseComponentType;
  headerSlot?: BaseComponentType;
  headerExtraSlot?: BaseComponentType;
  footSlot?: BaseComponentType;
  actionSlot?: BaseComponentType;
  attrs?: HTMLAttributes;
};
export class CardFactory {
  private defaultSlot: BaseComponentType = null;
  private coverSlot: BaseComponentType = null;
  private headerSlot: BaseComponentType = null;
  private headerExtraSlot: BaseComponentType = null;

  private footSlot: BaseComponentType = null;
  private actionSlot: BaseComponentType = null;

  private props: CardProps = {};
  private attrs: HTMLAttributes = {};

  constructor(param?: CardFactoryConstructorType) {
    if (param?.props) this.setProps(param.props);
    if (param?.defaultSlot) this.setDefault(param.defaultSlot);
    if (param?.coverSlot) this.setCover(param.coverSlot);
    if (param?.attrs) this.setAttrs(param.attrs);
    if (param?.headerSlot) this.setHeader(param.headerSlot);
    if (param?.headerExtraSlot) this.setHeaderExtra(param.headerExtraSlot);
    if (param?.footSlot) this.setFoot(param.footSlot);
    if (param?.actionSlot) this.setAction(param.actionSlot);
  }
  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }
  setProps(props: CardProps) {
    this.props = props;
    return this;
  }
  setDefault(component: BaseComponentType) {
    this.defaultSlot = component;
    return this;
  }
  setHeader(component: BaseComponentType) {
    this.headerSlot = component;
    return this;
  }
  setHeaderExtra(component: BaseComponentType) {
    this.headerExtraSlot = component;
    return this;
  }
  setCover(component: BaseComponentType) {
    this.coverSlot = component;
    return this;
  }

  setFoot(component: BaseComponentType) {
    this.footSlot = component;
    return this;
  }

  setAction(component: BaseComponentType) {
    this.actionSlot = component;
    return this;
  }
  create() {
    return (
      <NCard {...this.attrs} {...this.props}>
        {{
          default: () => getSlotsDom(this.defaultSlot),
          cover: () => getSlotsDom(this.coverSlot),
          header: () => getSlotsDom(this.headerSlot),
          "header-extra": () => getSlotsDom(this.headerExtraSlot),
          foot: () => getSlotsDom(this.footSlot),
          action: () => getSlotsDom(this.actionSlot),
        }}
      </NCard>
    );
  }
}
